<template>
  <div class="date-navigation">
    <div class="top">
      <div class="nav-left" @click="prevWeek">
        <span class="icon"><img src="@/assets/icons/schedule-btn-left.webp" alt="" /></span>
      </div>
      <div class="nav-center">
        <div
          class="date-item"
          v-for="(date, index) in weekDates"
          :key="index"
          @click="() => selectedDateHandle(index)"
          :class="index === dateIndex ? 'active' : ''"
        >
          <span class="match-count" v-show="dateIndex === index">({{ matchCount }}场比赛)</span>
          <div class="date" v-show="dateIndex !== index">{{ date.format('MM-DD') }}</div>
          <div class="day">{{ dayNames[date.day()] }}</div>
        </div>
      </div>
      <div class="nav-right" @click="nextWeek">
        <span class="icon"><img src="@/assets/icons/schedule-btn-left.webp" alt="" /></span>
      </div>
    </div>

    <div class="selected-date">
      <span class="selected-date-text">{{ weekDates[dateIndex].format('MM月DD日') }}</span>
      <div class="filters">
        <button class="filter" :class="type === 0 ? 'active' : ''" @click="() => changeType(0)">
          所有
        </button>
        <button
          class="filter"
          :class="type === 1 ? 'active' : ''"
          @click="() => changeType(1)"
          v-show="dateIndex === 0"
        >
          直播中
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { getScheduleList } from '@/api/home'
export default {
  name: 'DateNavigation',
  // props: {},
  data() {
    return {
      currentDate: dayjs(), // Start from today
      currentWeekStart: dayjs().startOf('week').add(1, 'day'), // Start on Monday
      selectedDate: dayjs(),
      matchCount: 577,
      dayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
      type: 1,
      dateIndex: 0
    }
  },
  mounted() {
    this.changeType(1)
  },
  computed: {
    weekDates() {
      // Returns an array of 7 dayjs objects representing the current week
      return Array.from({ length: 7 }, (_, i) => this.currentDate.add(i, 'day'))
    }
  },
  methods: {
    nextWeek() {
      this.currentDate = this.currentDate.add(7, 'day')
      this.selectedDate = this.currentDate // Optionally reset selected date to the new week
    },
    prevWeek() {
      this.currentDate = this.currentDate.subtract(7, 'day')
      this.selectedDate = this.currentDate // Optionally reset selected date to the new week
    },
    changeType(type) {
      this.type = type
      this.$emit('matchStatusHandle', type)
    },
    selectedDateHandle(index) {
      this.dateIndex = index
      this.changeType(0)
      this.$emit('selectDate', this.weekDates[index])
      const isToday = dayjs().isSame(dayjs(this.weekDates[index]), 'day')
      console.log(isToday, 'isToday');
      if (isToday) {
        this.type = 1
      }
      // this.selectedDate = this.weekDates[index]
    }
  }
}
</script>

<style scoped lang="scss">
.date-navigation {
  //   display: flex;
  //   align-items: center;
  //   justify-content: space-between;
  background: url('@/assets/imgs/schedule-header-bg.webp') no-repeat;
  //   border: 1px dashed #ccc;
  //   padding: 10px;
  //   background-color: #f9f9f9;
  border-radius: 10px;
  width: 940px;
  height: 120px;
  margin: 0 auto;
  background-size: 100% 100%;
  position: sticky;
  margin-bottom: 20px;
  top: 70px;
  .top {
    display: flex;
    align-items: center;
    padding: 14px 20px 8px;
  }

  .nav-left,
  .nav-right {
    display: flex;
    align-items: center;
    cursor: pointer;
  }

  .nav-left .icon,
  .nav-right .icon {
    font-size: 24px;
    img {
      width: 20px;
    }
  }
  .nav-right .icon {
    transform: rotate(180deg);
  }

  .match-count {
    // margin-left: 5px;
    font-weight: 500;
    font-size: 14px;
    color: #16b13a;
  }

  .nav-center {
    display: flex;
    justify-content: space-between;
    flex-grow: 1;
    margin: 0 20px;
  }

  .date-item {
    text-align: center;
    cursor: pointer;
    color: #525a79;
    width: 14.2%;
    .date {
      font-weight: 500;
      font-size: 14px;
    }

    .day {
      font-weight: 400;
      font-size: 12px;
    }
    &.active {
      color: #16b13a;
    }
  }

  .selected-date {
    display: flex;
    align-items: center;
    justify-content: space-between;
    // width: 200px;
    // width: 100%;
    padding: 10px 30px;
  }

  .selected-date-text {
    font-weight: 500;
    font-size: 18px;
    color: #16b13a;
  }

  .filters {
    display: flex;

    .filter {
      border: none;
      background-color: #f0f0f0;
      padding: 5px 10px;
      border-radius: 5px;
      font-size: 14px;
      margin-left: 10px;
      width: 80px;
      height: 30px;
      background: #eaf0ff;
      border-radius: 20px 20px 20px 20px;
      cursor: pointer;
      &.all {
        color: #999;
      }

      &.active {
        background: #16b13a;
        color: white;
      }
    }
  }
}
</style>
